package com.jeeplus.test.jlnu.admissionInfo.service.impl;

import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.RegistrarOffice;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.StudentInfoEntity;
import com.jeeplus.test.jlnu.admissionInfo.mapper.ExcelMapper;
import com.jeeplus.test.jlnu.admissionInfo.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private ExcelMapper excelMapper;

    @Override
    public void insertData(List<RegistrarOffice> dataList) {

        SimpleDateFormat ft = new SimpleDateFormat ( "yyyy-MM-dd" );
        // 使用 Stream 生成新列表
        List<AdmissionInfo> admissionInfos = dataList.stream()
                .map(registrarOffice -> {
                    AdmissionInfo admissionInfo = new AdmissionInfo();
                    admissionInfo.setExamineeNum(registrarOffice.getKsh());
                    admissionInfo.setCategory(registrarOffice.getKlmc());
                    admissionInfo.setAdmissionNum(registrarOffice.getZkzh());
                    admissionInfo.setStudentId(registrarOffice.getXh());
                    admissionInfo.setName(registrarOffice.getXm());
                    admissionInfo.setGender(registrarOffice.getXbmc());
                    try {
                        admissionInfo.setBirthday(ft.parse(registrarOffice.getCsny()));
                    } catch (ParseException e) {
                        admissionInfo.setBirthday(null);
                    }
                    admissionInfo.setPoliticalOutlook(registrarOffice.getZzmmmc());
                    admissionInfo.setNation(registrarOffice.getMzmc());
                    admissionInfo.setIdCard(registrarOffice.getSfzh());
                    admissionInfo.setPlanNature(registrarOffice.getJhxzmc());
                    // 设置录取学校为吉林师范大学
                    admissionInfo.setAdmissionSchools("吉林师范大学");
                    admissionInfo.setAdmissionAcademy(registrarOffice.getXy());
                    admissionInfo.setAdmissionMajor(registrarOffice.getLqzymc());
                    admissionInfo.setAdmissionTime(registrarOffice.getNf());
                    admissionInfo.setGradation(registrarOffice.getZycc());
                    admissionInfo.setEducationalSystem(registrarOffice.getZyxz());
                    // TODO: 学习形式字段暂时没有
                    // admissionInfo.setLearningForms(registrarOffice.get);
                    admissionInfo.setExamineeSort(registrarOffice.getKslbmc());
                    admissionInfo.setBatch(registrarOffice.getPcmc());
                    admissionInfo.setGraduationSchool(registrarOffice.getZxmc());
                    admissionInfo.setSourceArea(registrarOffice.getLydq());
                    admissionInfo.setAreaName(registrarOffice.getLydq());
                    admissionInfo.setHomeAddress(registrarOffice.getJtdz());
                    admissionInfo.setPostalCode(registrarOffice.getYzbm());
                    admissionInfo.setPhoneNum(registrarOffice.getLxdh());
                    admissionInfo.setOfficeId(registrarOffice.getOfficeId());
                    String admissionTime = admissionInfo.getAdmissionTime();
                    String educationalSystem = admissionInfo.getEducationalSystem();
                    int admissionTimeInt = Integer.parseInt(admissionTime);
                    int educationalSystemInt = Integer.parseInt(educationalSystem);

                    // 两个数字相加
                    int result = admissionTimeInt + educationalSystemInt;
                    // 创建一个 Calendar 对象，设置当前年份
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.YEAR, result);  // 设置新的年份
                    calendar.set(Calendar.MONTH, Calendar.JANUARY);  // 设置为1月（可选）
                    calendar.set(Calendar.DAY_OF_MONTH, 1);  // 设置为1号（可选）
                    // 将结果转换为字符串
                    Date resultDate = calendar.getTime();
                    admissionInfo.setExpectedGraduationDate(resultDate);

                    admissionInfo.setInfoSource("教务处招生数据");
                    // TODO: 信息源和备注字段暂时没有
                    // admissionInfo.setInfoSource();
                    // admissionInfo.setRemarks();
                    if(admissionInfo.getGradation().equals("本科")){
                        admissionInfo.setFlag("1");
                    }else if(admissionInfo.getGradation().equals("硕士")){
                        admissionInfo.setFlag("2");
                    }else {
                        admissionInfo.setFlag("3");
                    }

                    return admissionInfo; // 返回生成的 AdmissionInfo 对象
                }).collect(Collectors.toList()); // 收集到 admissionInfos 列表中


        // 插入录取信息表全部数据
        excelMapper.saveFirstData(admissionInfos);
        // 插入学籍信息表（只插入学号）
        excelMapper.saveSecondData(admissionInfos);
        // 插入毕业信息表（只插入学号）
//        excelMapper.saveThirdData(admissionInfos);
        // 插入就业信息表（只插入学号）
        excelMapper.saveFourth(admissionInfos);
        // 插入学位信息表（只插入学号）
        excelMapper.saveFifth(admissionInfos);
        // 插入学历信息表（只插入学号）
        excelMapper.saveSixth(admissionInfos);
        // 插入转递（邮寄信息表）
        excelMapper.saveSeventh(admissionInfos);
        // 档案信息表
//        excelMapper.saveEighth(admissionInfos);
        excelMapper.saveFourteenth(admissionInfos);
        excelMapper.saveFifteenth(admissionInfos);
        // 创建两个列表来存储不同 flag 的对象
        List<AdmissionInfo> flagOneList = new ArrayList<>();
        List<AdmissionInfo> otherFlagList = new ArrayList<>();

        for (AdmissionInfo admissionInfo : admissionInfos) {
            if ("1".equals(admissionInfo.getFlag())) {
                flagOneList.add(admissionInfo);
            } else {
                otherFlagList.add(admissionInfo);
            }
        }

        // 对于 flag 为 1 的对象调用保存方法
        if (!flagOneList.isEmpty()) {
            excelMapper.saveNinth(flagOneList);
            excelMapper.saveTenth(flagOneList);
            excelMapper.saveEleth(flagOneList);
            excelMapper.saveThirteenth(flagOneList);
        }

        // 对于 flag 为 2 或其他的对象调用保存方法
        if (!otherFlagList.isEmpty()) {
            excelMapper.saveTwelfth(otherFlagList);
            excelMapper.saveSixteenth(otherFlagList);
            excelMapper.saveSeventeenth(otherFlagList);
            excelMapper.saveEighteenth(otherFlagList);
        }


    }

    @Override
    public void deleteDatabase() {
        excelMapper.delete1();
        excelMapper.delete2();
        excelMapper.delete3();
        excelMapper.delete4();
        excelMapper.delete5();
        excelMapper.delete6();
        excelMapper.delete7();
        excelMapper.delete8();
        excelMapper.delete9();
        excelMapper.delete10();
        excelMapper.delete11();
        excelMapper.delete12();
        excelMapper.delete13();
        excelMapper.delete14();
        excelMapper.delete15();
        excelMapper.delete16();
        excelMapper.delete17();
        excelMapper.delete18();
    }

   /* @Override
    public void insertDataStu(List<StudentInfoEntity> dataList) {
        // 使用 Stream 生成新列表
        List<AdmissionInfo> admissionInfos = dataList.stream()
                .map(registrarOffice -> {
                    AdmissionInfo admissionInfo = new AdmissionInfo();
                    //admissionInfo.setExamineeNum(registrarOffice.getKsh());
                    admissionInfo.setAdmissionNum(registrarOffice.getExamNumber());
                    admissionInfo.setStudentId(registrarOffice.getStudentNumber());
                    admissionInfo.setName(registrarOffice.getName());
                    admissionInfo.setGender(registrarOffice.getGender());
                    admissionInfo.setBirthday(registrarOffice.getBirthDate());
                    admissionInfo.setPoliticalOutlook(registrarOffice.getPoliticalStatus());
                    admissionInfo.setNation(registrarOffice.getEthnicity());
                    admissionInfo.setIdCard(registrarOffice.getIdNumber());
                    admissionInfo.setPlanNature(registrarOffice.getSpecialProgram());
                    // 设置录取学校为吉林师范大学
                    admissionInfo.setAdmissionSchools("吉林师范大学");
                    admissionInfo.setAdmissionAcademy(registrarOffice.getCollege());
                    admissionInfo.setAdmissionMajor(registrarOffice.getMajorName());
                    //admissionInfo.setAdmissionTime(registrarOffice.getNf()); //年份没有
                    //admissionInfo.setGradation(registrarOffice.getZycc()); //专业层次没有
                    //admissionInfo.setEducationalSystem(registrarOffice.getZyxz());//专业性质没有
                    // TODO: 学习形式字段暂时没有
                    // admissionInfo.setLearningForms(registrarOffice.get);
                    admissionInfo.setExamineeSort(registrarOffice.getStudentCategory());
                    //admissionInfo.setBatch(registrarOffice.getPcmc());//批次没有
                    admissionInfo.setGraduationSchool(registrarOffice.getPreviousSchoolName());
                    admissionInfo.setSourceArea(registrarOffice.getCandidateSource());
                    admissionInfo.setAreaName(registrarOffice.getCandidateSource());
                    admissionInfo.setHomeAddress(registrarOffice.getResidenceAddress()); //这里用的户口地址
                    admissionInfo.setPostalCode(registrarOffice.getMailingZipCode());
                    admissionInfo.setPhoneNum(registrarOffice.getContactPhoneNumber());
                    // TODO: 信息源和备注字段暂时没有
                    // admissionInfo.setInfoSource();
                    // admissionInfo.setRemarks();

                    return admissionInfo; // 返回生成的 AdmissionInfo 对象
                }).collect(Collectors.toList()); // 收集到 admissionInfos 列表中
    }*/
}